package com.hzo.great.leisuregreat.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.hzo.great.leisuregreat.R;


public class DialogFactory {

    private static DialogFactory factory;
    private static Activity context;

    private static Dialog dialog;

    private static Dialog loadDialog;

    public static DialogFactory getInstance(Activity context) {
        DialogFactory.context = context;
        if (factory == null) {
            return new DialogFactory();
        }
        return factory;
    }

    public static void ShowLoadingIcon(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);

        dialog = new Dialog(context, R.style.CustomDialog);
//        listener.OnInitViewListener(view, dialog);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setWindowAnimations(0);
        window.setGravity(Gravity.CENTER);
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
//        if (height != 0)
            p.height = (int) (d.getHeight() * 0.4); // 高度设置为屏幕的0.4
//        if (width != 0)
            p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.9
        window.setAttributes(p);
        if(!dialog.isShowing())
            dialog.show();
    }

    /**
     * 关闭loading框
     */
    public static void closeLoadDialog() {
        if (loadDialog != null) {
            try {
                loadDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("之前的activity被销毁，dialog关闭异常。该报错可以忽略");
            }
            loadDialog = null;
        }
    }

    /**
     * @param layout        layoutRes
     * @param dialogStyle   dialogStyle dialog 样式
     * @param windowAnimStyle         windowAnimStyle window显示动画
     * @param windowGravity window 显示位置 （Gravity.CENTER_VERTICAL...）
     * @param width         宽度百分比（0~1）
     * @param height        高度百分比（0~1）
     * @param listener      子控件点击事件监听
     */
    public static void showDialog(Context context, int layout, int dialogStyle, int windowAnimStyle, int windowGravity, float width, float height, DialogListener listener) {
        View view = LayoutInflater.from(context).inflate(layout, null);

        dialog = new Dialog(context, dialogStyle);
        listener.OnInitViewListener(view, dialog);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setWindowAnimations(windowAnimStyle);
        window.setGravity(windowGravity);
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        if (height != 0)
            p.height = (int) (d.getHeight() * height); // 高度设置为屏幕的0.4
        if (width != 0)
            p.width = (int) (d.getWidth() * width); // 宽度设置为屏幕的0.9
        window.setAttributes(p);
        if(!dialog.isShowing())
            dialog.show();
    }


    public static void showAllDialog(Context context, int layout, int dialogStyle, int windowAnimStyle, int windowGravity, float width, float height, DialogListener listener) {

        View view = LayoutInflater.from(context).inflate(layout, null);
        dialog = new Dialog(context, dialogStyle);
        listener.OnInitViewListener(view, dialog);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setWindowAnimations(windowAnimStyle);
        window.setGravity(windowGravity);
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) d.getHeight();
        p.width = (int) d.getWidth();
//        if (height != 0)
//            p.height = (int) (d.getHeight() * height); // 高度设置为屏幕的0.4
//        if (width != 0)
//            p.width = (int) (d.getWidth() * width); // 宽度设置为屏幕的0.9
        window.setAttributes(p);
        if(!dialog.isShowing())
            dialog.show();
    }

    public static void ButtonDialog(Context context, int layout, int dialogStyle, int windowAnimStyle, int windowGravity, float width, float height, DialogListener listener) {
        View view = LayoutInflater.from(context).inflate(layout, null);

        dialog = new Dialog(context, dialogStyle);
        listener.OnInitViewListener(view, dialog);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setWindowAnimations(windowAnimStyle);
//        window.setGravity(windowGravity);
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) d.getHeight();
        p.width = (int) d.getWidth();
        p.gravity = Gravity.BOTTOM;
        window.setAttributes(p);
        if(!dialog.isShowing())
            dialog.show();
    }
    public void showPreImgDialog(int layout, int dialogStyle, int windowAnimStyle, int windowGravity, float width,
                                 float height, DialogListener listener) {

        View view = LayoutInflater.from(context.getApplicationContext()).inflate(layout, null);
        dialog = new Dialog(context, dialogStyle);
        Dialog dialog = new Dialog(context, dialogStyle);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setWindowAnimations(windowAnimStyle);
        window.setGravity(windowGravity);
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        if (height != 0)
            p.height = (int) (d.getHeight() * height); // 高度设置为屏幕的0.4
        if (width != 0)
            p.width = (int) (d.getWidth() * width); // 宽度设置为屏幕的0.9
        window.setAttributes(p);
        if(!dialog.isShowing())
            dialog.show();
    }


    public static void dissmissDialog() {
        if (dialog != null && dialog.isShowing() && !context.isFinishing()) {
            dialog.dismiss();
            dialog = null;
        }
        context = null;
    }

    public static void CloseDialog(){
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public interface DialogListener {
        void OnInitViewListener(View v, Dialog dialog);
    }

    public interface PayDialogListener {
        /**
         * 支付宝支付
         **/
        void OnAliPayClick();

        void OnAliNetPayClick();

        /**
         * 余额支付
         **/
        void OnExtraPayClick();

        void onDialogDissmiss();

    }

}
